package avicit.bdp.dgs.standard.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.service.dto.CommonTypeDTO;
import avicit.bdp.common.service.service.CommonTypeService;
import avicit.bdp.common.utils.CheckParaUtils;
import avicit.bdp.core.enums.DataElementDataTypeEnum;
import avicit.bdp.core.enums.DataElementStandardTypeEnum;
import avicit.bdp.core.enums.DataStateEnum;
import avicit.bdp.dgs.standard.dao.DataElementHistoryDao;
import avicit.bdp.dgs.standard.dto.DataElementHistoryDto;
import avicit.bdp.dgs.standard.dto.LookupTableDto;
import avicit.bdp.dgs.standard.vo.DataElementHistoryDIffDetail;
import avicit.bdp.dgs.standard.vo.DataElementHistoryDiffVo;
import avicit.bdp.dgs.standard.vo.DataElementHistoryVo;
import com.github.pagehelper.Page;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author mayi 数据元历史版本服务层
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DataElementHistoryService
    extends BaseService<DataElementHistoryDao, DataElementHistoryDto> {
  
  @Resource
  private CommonTypeService commonTypeService;
  @Resource
  private LookupTableService lookupTableService;

  public void deleteByDataElementId(String dataElementId) {
    this.mapper.deleteByDataElementId(dataElementId);
  }

  /**
   * 通过数据元ID查询最大历史记录版本
   *
   * @param dataElementId 数据元ID
   * @return 最大历史记录版本
   */
  String searchMaxVersionByDataElementId(String dataElementId) {
    return this.mapper.searchMaxVersionByDataElementId(dataElementId);
  }

  /**
   * 通过数据元ID分页查询
   *
   * @param dataElementId 数据元ID
   * @return 分页数据
   */
  public Page<DataElementHistoryVo> getVersionPage(String dataElementId) {
    return this.mapper.getVersionPage(dataElementId);
  }

  public DataElementHistoryDto selectByDataElementIdAndVersion(
      String dataElementId, String dataElementVersion) {
    DataElementHistoryDto dataElementHistoryDto =
        this.mapper.selectByDataElementIdAndVersion(dataElementId, dataElementVersion);
    if (dataElementHistoryDto != null) {
      fullEnumInfo(dataElementHistoryDto);
    }
    return dataElementHistoryDto;
  }

  private void fullEnumInfo(DataElementHistoryDto dataElementDto) {
    dataElementDto.setStatusString(DataStateEnum.getDescByCode(dataElementDto.getStatus()));
    dataElementDto.setDataTypeString(
        DataElementDataTypeEnum.getDescByCode(dataElementDto.getDataType()));
    dataElementDto.setStandardTypeString(
        DataElementStandardTypeEnum.getDescByCode(dataElementDto.getStandardType()));
    
    if (StringUtils.isNotBlank(dataElementDto.getTypeId())) {
      CommonTypeDTO commonTypeDTO = commonTypeService.selectByPrimaryKey(dataElementDto.getTypeId());
      if (commonTypeDTO != null) {
        dataElementDto.setTypeName(commonTypeDTO.getName());
      }
    }
    if (StringUtils.isNotBlank(dataElementDto.getLookupTableId())) {
      LookupTableDto lookupTableDto = lookupTableService.selectByPrimaryKey(dataElementDto.getLookupTableId());
      if (lookupTableDto != null) {
        dataElementDto.setLookupTableName(lookupTableDto.getName());
      }
    }
  }

  public int batchInsert(List<DataElementHistoryDto> list) {
    return this.mapper.batchInsert(list);
  }

  public boolean batchUpdateStatus(List<DataElementHistoryDto> list) {
    return this.mapper.batchUpdateStatus(list) > 0;
  }

  public int countByStatus(String dataElementId, int status) {
    return this.mapper.countByStatus(dataElementId, status);
  }

  public DataElementHistoryDiffVo diff(String currentVersionId, String targetVersionId) {
    DataElementHistoryDiffVo diffVo = new DataElementHistoryDiffVo();

    DataElementHistoryDto currentVersionInfo = this.mapper.selectByPrimaryKey(currentVersionId);
    CheckParaUtils.checkDbObjectValidity(
        currentVersionInfo, logger, String.format("没有查到数据，请确保id:%s的数据是否存在", currentVersionId));
    fullEnumInfo(currentVersionInfo);

    DataElementHistoryDto targetVersionInfo = this.mapper.selectByPrimaryKey(targetVersionId);
    CheckParaUtils.checkDbObjectValidity(
        targetVersionInfo, logger, String.format("没有查到数据，请确保id:%s的数据是否存在", targetVersionId));
    fullEnumInfo(targetVersionInfo);

    diffVo.setCurrentVersionInfo(currentVersionInfo);
    diffVo.setTargetVersionInfo(targetVersionInfo);
    diffVo.setdIffDetail(new DataElementHistoryDIffDetail(currentVersionInfo, targetVersionInfo));

    return diffVo;
  }
}
